package com.bzframework.pro.web;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @Author bingzhong
 * @Date 2025/4/26 17:12
 * @Description
 */
public class AppTokenUtils {

    public static final Long   EXPIRES_IN = 1 * 3600 * 1000L;
    public static final String TOKEN_APPID = "680c9ec8a923cd08bdc91ea0";
    public static final String TOKEN_APPSECRET = "5rIDHGgwwD72tRW1IJzyFITTlOVmwQkB";

    public static String genAppToken(String appId, String appSecret) {
        if (StringUtils.isEmpty(appId) || StringUtils.isEmpty(appSecret)) {
            return null;
        }

        Algorithm algorithm = Algorithm.HMAC256(appSecret);
        long timestamp = System.currentTimeMillis();
        Date issuedAt = new Date(timestamp);
        Date expiresAt = new Date(timestamp + EXPIRES_IN);

        return JWT.create()
                  .withClaim(TOKEN_APPID, appId)
                  .withIssuedAt(issuedAt)
                  .withExpiresAt(expiresAt)
                  .sign(algorithm);
    }

    public static String genAppToken() {
        return genAppToken(TOKEN_APPID, TOKEN_APPSECRET);
    }

    public static void main(String[] args) {
        String appToken = genAppToken();
        System.out.println("appToken = " + appToken);
    }


}
